/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.db.SqlManager;
import com.exception.DataException;
import com.javabeans.Matricula;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Administrador
 */
public class AdminMatricula implements MatriculaManager{
    private Connection c;
    public AdminMatricula(Connection c){
        this.c = c;
    }
    @Override
    public void saveMatricula(Matricula m) throws DataException {
        PreparedStatement preSt = null;
        String query = "";
        try{
            query="INSERT INTO MATRICULA VALUES("+m.getIdGestion();
            query+= ", '"+m.getIdMatricula();
            query+= "', "+m.getIdCarrera();
            query+= ", "+m.getIdEstudiante();
            query+= ", "+m.getIdPeriodo();
            query+= ", "+m.getIdTransaccion();
            query+= ", "+m.isActivo()+")";            
            preSt = c.prepareStatement(query);
            preSt.executeUpdate();
        }catch(SQLException ex){
            throw new DataException(" AdminMatricula -> saveMatricula(Matricula m) "+ex.getMessage());
        }finally{
            SqlManager.closePreSt(preSt);
        }
    }
    @Override
    public void saveMatriculas(List<Matricula> matriculas) throws DataException {
        PreparedStatement preSt = null;
        int i = 0;
        String query = "INSERT INTO MATRICULA VALUES";
        try{
            for(Matricula m : matriculas){
                query += "("+m.getIdGestion();
                query += ",'"+m.getIdMatricula()+"',"+m.getIdCarrera();
                query += ","+m.getIdEstudiante();
                query += ","+m.getIdPeriodo();
                query += ","+m.getIdTransaccion();
                query += ","+m.isActivo()+"),";
                i++;
            }
            if(i > 0){
                i = query.lastIndexOf(",");
                query = query.substring(0, i);
            }            
            preSt = c.prepareStatement(query);
            preSt.executeUpdate();
        }catch(SQLException e){
            throw new DataException(" AdminMatricula -> saveMatriculas(List<Matricula> matriculas) "+e.getMessage());
        }finally{
            SqlManager.closePreSt(preSt);
        }
    }
    
    @Override
    public boolean codigoValido(int idGestion, String codigoMatri, int idCarrera, int codSis) throws DataException{
        boolean res = false;
        ResultSet rs = null;
        PreparedStatement preSt = null;
        try{
            String query = "SELECT IDGESTION";
            query += " FROM MATRICULA";
            query += " WHERE IDGESTION="+idGestion+" AND IDMATRICULA='"+codigoMatri+"'";
            query += " AND IDCARRERA = "+idCarrera+" AND IDESTUDIANTE="+codSis+" LIMIT 1";            
            preSt = c.prepareStatement(query);
            rs = preSt.executeQuery();
            if(rs.next()){
                res = true;
            }
        }catch(SQLException e){
            throw new DataException(" AdminVentaMatricula -> codigoValido(int idGestion, String codigo, int idCarrera, int codSis) e = "+e.getMessage());
        }finally{
            SqlManager.closeRs(rs);
            SqlManager.closePreSt(preSt);
        }
        return res;
    }
}
